package 链表;

public class _03_移除链表元素 {
	public ListNode removeElements(ListNode head, int val) {
		ListNode newHead = new ListNode(-1);
		newHead.next = head;
		while (head != null) {
			if (head.val == val) {
				newHead.next = head.next;
				head = head.next;
			} else {
				newHead = newHead.next;
				head = head.next;
			}
		}
		return newHead.next;

//		ListNode newHead = new ListNode(-1);
//		newHead.next = head;
//		
//		ListNode prev = newHead;
//		ListNode curr = head;
//		
//		while (curr != null) {
//			if (curr.val == val) {
//				prev.next = curr.next;
//				curr = curr.next;
//			} else {
//		        prev = prev.next;
//				curr = curr.next;
//			}
//		}
//		return newHead.next;
	}

	public ListNode removeElementsFun(ListNode head, int val) {
		if (head == null) {
			return null;
		}
 head.next = removeElements(head.next, val);

		return head.val == val?head.next:head;
	}
}
